﻿#include "PostTools.h"
#include <vtkWindowToImageFilter.h>
#include <vtkImageResize.h>
#include <vtkPNGWriter.h>
#include <vtkSmartPointer.h>

namespace pst
{
    void PostTools::saveJPEGImageFromVTKScreen(vtkWindow* window, int width, int height, QString fileName, bool autoSize)
    {
        vtkNew<vtkWindowToImageFilter> report_windowToImageFilter;
        vtkNew<vtkImageResize> report_resize;
        vtkNew<vtkPNGWriter> report_writer;

        report_windowToImageFilter->SetInput(window);
        report_resize->SetInputConnection(report_windowToImageFilter->GetOutputPort());
        if (autoSize)
        {
            report_windowToImageFilter->Update();
            auto dimension = report_windowToImageFilter->GetOutput()->GetDimensions();
            report_resize->SetOutputDimensions(dimension[0], dimension[1], -1);
        }
        else
        {
            report_resize->SetOutputDimensions(width, height, -1);
        }
        report_resize->Update();
        report_writer->SetFileName(fileName.toLocal8Bit().data());
        report_writer->SetInputConnection(report_resize->GetOutputPort());
        report_writer->Write();
    }
}
